package com.soft.javase.day11.serial;

import java.io.*;

/**
 * @author yzg
 * @desc TODO
 * @date 2025/5/9
 */
public class SerialDemo2 {
    public static void serial(){
        try(
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("user.txt"))
        ){
            User user = new User("张三",18);
            oos.writeObject(user);
            oos.flush();
            System.out.println("序列化成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void unSerial(){
        try(
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream("user.txt"))
        ){
            User user = (User) ois.readObject();
            System.out.println(user);
        }catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        serial();
        unSerial();
    }
}
